package tables

import (
	"gorm.io/gorm"
	"time"
)

type Order struct {
	ID     string `gorm:"type:varchar(36);not null;primary_key:autoIncrement:false"`
	UserID int32  `gorm:"column:user_id;type:int;index:idx_user_id"`
	Status int32  `gorm:"column:order_status;type:tinyint;not null;default:0;comment:'0为待处理, 1为处理中, 2为处理完成'"`
	//OrderGoods []OrderGoods   `gorm:"foreignKey:OrderID;references:ID" json:"order_goods"`
	CreatedAt *MyTime        `gorm:"column:create_time;"`
	UpdatedAt *MyTime        `gorm:"column:update_time;"`
	DeleteAt  gorm.DeletedAt `gorm:"index;"`

	OrderGoodsID    string   `gorm:"column:order_goods_id;type:varchar(10);index;not null"`
	Goods           Goods    `gorm:"foreignKey:OrderGoodsID;references:GoodsID"`
	Count           int32    `gorm:"column:order_goods_count;type:int;not null"`
	Color           string   `gorm:"column:order_goods_color;type:varchar(100);not null;"`
	GoodsImage      string   `gorm:"column:order_goods_image;type:varchar(1000);commit:'保存主图';not null"`
	GoodsName       string   `gorm:"column:order_goods_name;type:varchar(80);index;not null"`
	Platform        string   `gorm:"column:order_platform;type:varchar(10);not null"`
	DistributorName string   `gorm:"column:order_distributor_name;type:varchar(20);not null"`
	CustomerName    string   `gorm:"column:order_customer_name;type:varchar(50);not null"`
	Email           string   `gorm:"column:order_email;type:varchar(200);not null"`
	Remark          string   `gorm:"column:order_remark;type:text;not null"`
	Files           GormList `gorm:"column:order_files;type:json"`
}

func (o *Order) TableName() string {
	return "order"
}

// 钩子函数
func (o *Order) BeforeCreate(tx *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	o.CreatedAt = &MyTime{Time: fptTime}
	o.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}

func (o *Order) BeforeUpdate(scope *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	o.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}

// es钩子函数

//type OrderGoods struct {
//	OrderId string `gorm:"type:varchar(36);not null;index:idx_order"`
//	GoodsID string `gorm:"type:varchar(10);index;not null"`
//
//	GoodsName  string `gorm:"type:varchar(80);index;not null"`
//	GoodsImage string `gorm:"type:varchar(1000);commit:'保存主图';not null"`
//	GoodsPrice int32  `gorm:"type:int;not null"`
//	Count      int32  `gorm:"type:int;not null"`
//	Size       string `gorm:"type:varchar(50);not null;default:'standard model'"`
//	Color      string `gorm:"type:varchar(50);not null;"`
//	BaseModel
//}
//
//func (og *OrderGoods) TableName() string {
//	return "order_goods"
//}
